package tps12.axwax.server.util;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * Wrapper around <code>HttpServletRequest</code> that honors X-HTTP-Method-Override
 * header for POST requests. 
 * 
 * @author Travis
 *
 */
public class CompatibleHttpServletRequest extends HttpServletRequestWrapper {
	private final static String METHOD_POST = "POST";
	private final static String HEADER_OVERRIDE = "X-HTTP-Method-Override";
	
	private final String method;
	
	/**
	 * Constructs a compatible request wrapper object. If the request method is
	 * POST then the value of X-HTTP-Method-Override, if any, will be used in place
	 * of the actual method when calling <code>getMethod()</code>.
	 * 
	 * All other calls are forwarded unchanged to the internal request object.
	 * 
	 * @param req The internal request object.
	 */
	public CompatibleHttpServletRequest(HttpServletRequest req) {
		super(req);
		method = req.getMethod().equals(METHOD_POST) ?
			req.getHeader(HEADER_OVERRIDE) :
			req.getMethod();
	}

	/**
	 * Gets the method, potentially as modified by the X-HTTP-Method-Override header.
	 */
	@Override
	public String getMethod()
	{ 
		return method != null ? method : super.getMethod(); 
	}

}
